﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Century.Web.Security.Attributes;

using Telerik.Web.Mvc;

using Century.Web.UI.MVC;
using System.Web.Security;
using Century.Model;
using Century.Business.CMS;
namespace Century.Admin.Controllers
{
    [Authorization(Group = "", Name = "Administration")]
    public class RoleActionModuleManagementController : CenturyAdminBaseController
    {
        private ISecurityService securityService {get;set;}

        public RoleActionModuleManagementController(ISiteService siteService, ISecurityService securityService) : base(siteService) 
        {
            this.securityService = securityService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetRoleList()
        {
            var model = new GridModel
            {
                Data = Roles.GetAllRoles()// roleBSO.GetAll().Select(e => new { e.Id, e.RoleName, e.DisplayName })
            };
            return View(model);
        }

        [GridAction]
        public ActionResult InsertRole(string role)
        {
            Roles.CreateRole(role);
            var d = new GridModel
            {
                Data = Roles.GetAllRoles()//.Select(e => new { e.Id, e.RoleName, e.DisplayName })
            };
            return View(d);
        }

        [GridAction]
        public ActionResult GetPermissionsForRole(string role)
        {
            List<ActionModule> actionModule = securityService.GetAllActionModules();
            var confDetails = actionModule.Select(c =>
                new
                {
                    c.Id,
                    c.Name,
                    c.DisplayName,
                    c.RequiredAdministrator,
                    RoleHasThisPermission = c.ActionModuleRoles.Any(a => a.RoleName == role)
                });
            return View(new GridModel(confDetails));
        }

        public string ChangeRolePermission(string role, int actionModuleId, bool roleHasThisActionModule)
        {
            if (roleHasThisActionModule)
            {
                securityService.AddActionModuleRole(role, actionModuleId);
                //actionModule.ActionModuleRoles.Add(new ActionModuleRole { RoleName = role });
            }
            else
            {
                securityService.RemveActionModuleRole(role, actionModuleId);
            }
            return string.Empty;
        }

    }
}
